package com.scedu.springcloud.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.scedu.springcloud.springcloud.entity.CommonResult;
import com.scedu.springcloud.springcloud.entity.Payment;
import com.scedu.springcloud.springcloud.service.OrderFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "order_defaultFallback")
public class OrderController {
    @Autowired
    OrderFeignService orderFeignService;


    @GetMapping(value = "consumer/create")
    public CommonResult create(Payment payment){
        CommonResult commonResult =orderFeignService.create(payment);
        log.info(String.valueOf(commonResult));
        return commonResult;
    }

    @GetMapping(value = "consumer/get/{id}")
    @HystrixCommand(fallbackMethod = "getDefaultPayment")
    public CommonResult getPaymentById(@PathVariable("id") Long id){

        return  orderFeignService.getPaymentById(id);


    }
    //熔断时返回的方法
    public CommonResult getDefaultPayment(@PathVariable("id") Long id){


        log.info("熔断，默认回调函数");
        return  new CommonResult(444,"查询数据失败,id",id);

    }
    @GetMapping(value = "/consumer/timeout")
    @HystrixCommand(fallbackMethod = "defaultTimeout")
    public String timeout(){

        return orderFeignService.timeout();
    }
    //超时熔断时返回的方法
    public String defaultTimeout(){
         return "服务出错，请稍后重试。";

    }
    //下面是全局的fallback方法
    public String order_defaultFallback(){
        return "服务出错，请稍后重试。";

    }

}
